import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  // namespaced: true,
  state: {
    goodList: [
      {
        name: '诸葛亮',
        price: 1000,
        num: 1,
        checked: false
      },
      {
        name: '蔡文姬',
        price: 1500,
        num: 1,
        checked: false
      },
      {
        name: '妲己',
        price: 2000,
        num: 1,
        checked: false
      },
      {
        name: '鲁班',
        price: 2200,
        num: 1,
        checked: false
      }
    ]
  },
  getters: {
    sum(state) {
      console.log(state);
      const arr = state.goodList.filter(item => item.checked)
     return arr.reduce((sum,item) => sum + item.price * item.num,0)
    }
  },
  mutations: {
    changeNum(state,payload) {
      console.log(payload);
      state.goodList[payload.index].num = payload.num
    },
    delList(state, payload) {
      console.log(payload);
       state.goodList.splice(payload.index,1)
    }
  },
  actions: {
  },
  modules: {
  }
})
